package com.jushu.storbox.view.resolve;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jushu.storbox.R;
import com.jushu.storbox.actions.TeamSpaceAction;
import com.jushu.storbox.adapter.TeamListAdapter;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.entities.TeamSpaceInfo;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.newfragment.argument.FileActionArgs;
import com.jushu.storbox.newfragment.argument.TeamListActionArgs;
import com.jushu.storbox.operation.group.TeamspaceOperation;
import com.jushu.storbox.vo.PagedList;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.kennyc.view.MultiStateView;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class TeamListResolve
		extends ListDisplayResolve<TeamSpaceInfo, TeamspaceOperation<TeamSpaceInfo>, TeamListAdapter> {

	private Stack<FileActionArgs> folderStack = new Stack<FileActionArgs>();
	private TeamListActionArgs teamListArgs = new TeamListActionArgs();
	private ProgressDialog waitDialog;
	private List<TeamSpaceInfo> datas = new ArrayList<TeamSpaceInfo>();
	private MultiStateView multiStateView;

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			multiStateView.setViewState(MultiStateView.ViewState.LOADING);
			loadDataList();
		}
	};

	public TeamListResolve(Activity activity, TeamListActionArgs teamListArgs) {
		super(activity);
		this.teamListArgs = teamListArgs;
	}

	public TeamListResolve(Activity activity, TeamListActionArgs teamListArgs, MultiStateView multiStateView) {
		super(activity);
		this.teamListArgs = teamListArgs;
		this.multiStateView = multiStateView;
		multiStateView.getView(MultiStateView.ViewState.ERROR).findViewById(R.id.button1).setOnClickListener(listener);
	}

	public void initComponent(PullToRefreshListView pullToRefreshListView) {
		super.initComponent(pullToRefreshListView);
		waitDialog = new ProgressDialog(context);
		waitDialog.setMessage(context.getResources().getString(R.string.loading));
	}

	@Override
	protected TeamListAdapter initTemplateAdapter() {
		return new TeamListAdapter(context, datas);
	}

	@Override
	protected TeamspaceOperation<TeamSpaceInfo> initAdapterOperation() {
		return operation;
	}

	@Override
	protected void clickItem(View clickView, int position, long id, TeamSpaceInfo data) {
		openTeamspace(data);
	}

	public Stack<FileActionArgs> getFolderStack() {
		return folderStack;
	}

	public void getMoreDataList() {
		new TeamSpaceAction().getMoreTeamSpaceListFromServer(context, teamListArgs.getPageIndex(), msgHandler);
	}

	public void loadDataList() {
		teamListArgs.setPageIndex(Constant.FIRST_PAGE_INDEX);
		new TeamSpaceAction().getTeamSpaceListFromServer(context, teamListArgs.getPageIndex(), msgHandler);
	}

	protected void updateCurrentFolder(String ownerId, String folderId, String folderName) {
	}

	public void openTeamspace(TeamSpaceInfo info) {
		updateCurrentFolder(info.getTeamSpaceId(), Constant.ROOT_FOLDER_ID, info.getName());
	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			TeamListAdapter.ViewHolder holder = (TeamListAdapter.ViewHolder) arg1.getTag();
			final TeamSpaceInfo info = holder.getmInfo();
			openTeamspace(info);
		}
	};

	public void addData(TeamSpaceInfo fileInfo) {
		List<TeamSpaceInfo> list = new ArrayList<TeamSpaceInfo>();
		list.add(fileInfo);
		adapter.addDatas(list);
	}

	TeamspaceOperation<TeamSpaceInfo> operation = new TeamspaceOperation<TeamSpaceInfo>() {
	};

	ClientExceptionRelateHandler msgHandler = new ClientExceptionRelateHandler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.obj instanceof ClientException && null != multiStateView) {
				multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
			} else if (null != multiStateView) {
				multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
			}

			switch (msg.what) {
			case MessageCode.TEAMSPACE_LIST_COMPLETE:
				handlerLoadList(msg);
				if (waitDialog.isShowing()) {
					waitDialog.dismiss();
				}
				break;
			case MessageCode.TEAMSPACE_OPEN_ROOT:
				// PagedList<FileFolderInfo> resource =
				// (PagedList<FileFolderInfo>)msg.obj;
				// ((IFileList)context).updateCurrentFolder(resource.getOwnerId(),
				// resource.getPagedFolderId(), resource.getPagedFolderName());
				break;
			default:
				if (waitDialog.isShowing()) {
					waitDialog.dismiss();
				}
				super.handleMessage(msg);
			}
		}

		@Override
		protected void onHandlerBefore(Message msg, String errorCode) {
			onRefreshComplete();
			super.onHandlerBefore(msg, errorCode);
		}

		@Override
		public Context getContext() {
			return context;
		}

		private void handlerLoadList(Message msg) {
			@SuppressWarnings("unchecked")
			PagedList<TeamSpaceInfo> pagedList = (PagedList<TeamSpaceInfo>) msg.obj;
			List<TeamSpaceInfo> pagedData = pagedList.getPagedList();
			onRefreshComplete();
			if (teamListArgs.getPageIndex() == Constant.FIRST_PAGE_INDEX) {
				adapter.cleanListDatasOnly();
			}
			if (pagedData == null) {
				return;
			}
			adapter.addDatas(pagedData);
			teamListArgs.incPageIndex();
			// unlock screen
		}
	};
}
